package com.voxel.sense.infomsg.module.firm.details.institution.list;

import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import com.example.com.common.app.PresenterFragment;
import com.example.com.common.utils.NetworkUtils;
import com.example.com.common.view.EmptyView;
import com.example.com.common.view.recycler.RecyclerAdapter;
import com.voxel.sense.infomsg.R;
import com.voxel.sense.infomsg.module.firm.details.FirmDtlInfoListContract;
import com.voxel.sense.infomsg.module.firm.details.institution.data.InstitutionDataActivity;
import com.voxel.sense.infomsg.view.decoration.ItemDivider;
import com.voxel.sense.infomsg.view.decoration.LinearItemDecoration;

import java.util.List;

import butterknife.BindView;
import cyy.example.com.facotry.constants.IntentConstants;
import cyy.example.com.facotry.model.api.response.InstitutionEntity;

/**
 * Created by ChenYiYao on 2018/7/25.
 * 站位列表
 */
public class InstitutionFrag extends PresenterFragment<FirmDtlInfoListContract.Presenter> implements
        FirmDtlInfoListContract.View<List<InstitutionEntity>> {
    @BindView(R.id.recycle_view)
    RecyclerView recycle_view;
    @BindView(R.id.empty_view)
    EmptyView empty_view; //空数据界面
    private String mCompanyid;
    private RecyclerAdapter mAdapter;


    @Override
    protected FirmDtlInfoListContract.Presenter initPresenter() {
        return new InstitutionPresenter(this);
    }

    @Override
    protected int getContentLayoutId() {
        return R.layout.activity_institution;
    }

    @Override
    protected void initArgs(Bundle bundle) {
        super.initArgs(bundle);
        if (bundle == null)
            return;
        mCompanyid = bundle.getString(IntentConstants.STRING_TYPE);
    }

    @Override
    protected void initWidget(View root) {
        super.initWidget(root);
        mAdapter = new InstitutionAdapter(new RecyclerAdapter.AdapterListenerImpl<InstitutionEntity>() {
            @Override
            public void onItemClick(RecyclerAdapter.ViewHolder holder, InstitutionEntity entity) {
                super.onItemClick(holder, entity);
                InstitutionDataActivity.show(getContext(), entity.getSiteCode());
            }
        });

        recycle_view.setLayoutManager(new LinearLayoutManager(getContext()));
        recycle_view.addItemDecoration(new LinearItemDecoration(R.drawable.recycle_divider_1dp_pink));
        recycle_view.setAdapter(mAdapter);
        empty_view.bind(recycle_view);
        empty_view.setRefreshBtnVisible(false);
    }

    @Override
    protected void initData() {
        super.initData();
        mPresenter.doLoadData(mCompanyid);
    }


    @Override
    public RecyclerAdapter<List<InstitutionEntity>> getRecyclerViewAdapter() {
        return mAdapter;
    }

    @Override
    public void onAdapterDataChanged() {
        boolean b = mAdapter.getItemCount() > 0;
        if (b) {
            empty_view.triggerOk();
        } else if (!NetworkUtils.isConnected()) {
            empty_view.triggerNetError();
        } else {
            empty_view.triggerEmpty();
        }
    }

    @Override
    public void showEmptyView(String desc, boolean btnVisible) {
        empty_view.triggerError(desc);
    }

    @Override
    public void showLoading(String str) {
        empty_view.triggerLoading();
    }
}
